<template>
  <a-modal
    title="新增其它数据"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="文件名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入文件名称" v-decorator="['title', {rules: [{required: true, message: '请输入文件名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="文件大小 kb"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入文件大小 kb" v-decorator="['size', {rules: [{required: true, message: '请输入文件大小 kb！'}]}]" />
        </a-form-item>
        <a-form-item
          label="文件大小"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入文件大小" v-decorator="['sizeInfo', {rules: [{required: true, message: '请输入文件大小！'}]}]" />
        </a-form-item>
        <a-form-item
          label="文件扩展名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入文件扩展名" v-decorator="['extension', {rules: [{required: true, message: '请输入文件扩展名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据条数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据条数" v-decorator="['dataSize', {rules: [{required: true, message: '请输入数据条数！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据类型" v-decorator="['type', {rules: [{required: true, message: '请输入数据类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="标签"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入标签" v-decorator="['tagId', {rules: [{required: true, message: '请输入标签！'}]}]" />
        </a-form-item>
        <a-form-item
          label="用户id,此字段不为空表示改文件为用户经营数据"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入用户id,此字段不为空表示改文件为用户经营数据" v-decorator="['userId', {rules: [{required: true, message: '请输入用户id,此字段不为空表示改文件为用户经营数据！'}]}]" />
        </a-form-item>
        <a-form-item
          label="0: 正常 1: 删除 2: 导入失败"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入0: 正常 1: 删除 2: 导入失败" v-decorator="['status', {rules: [{required: true, message: '请输入0: 正常 1: 删除 2: 导入失败！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { otherDataRecordAdd } from '@/api/modular/main/otherdatarecord/otherDataRecordManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            otherDataRecordAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
